import {createRouter, createWebHashHistory} from 'vue-router'

import Login from "@/components/Login";
import Home from "@/components/Home";
import Welcome from "@/components/Welcome";
import Users from "@/components/Users";
import AddUser from "@/components/user/AddUser";
import Rights from "@/components/power/rights/Rights";
import Roles from "@/components/power/roles/Roles";

const routes = [
    {path: '/login', component: Login},
    {path: '/', redirect: '/login'},
    {
        path: '/home', component: Home,
        redirect: '/welcome',
        children: [
            {path: '/welcome', component: Welcome},
            {path: '/users', component: Users},
            {path: '/adduser', component: AddUser},
            {path: '/rights', component: Rights},
            {path: '/roles', component: Roles},

        ]
    },

]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    //登录页放行
    if (to.path === '/login') return next()
    //
    const token = window.sessionStorage.getItem('token');
    if (!token) return next('/login')

    next()


})

export default router
